package docPlum.diagram.application;

import org.eclipse.core.internal.resources.Resource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.ui.wizard.AbstractExampleInstallerWizard.ProjectDescriptor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class DocPlumModelCreationPage extends WizardPage {

	private Text diagramNameEditor;

	private Text folderPathEditor;

	private Text docCoreEditor;

	private Text plumPLEditor;

	private IStructuredSelection structuredSelection;

	public URI getDomainModelURI() {
		IPath path = Path.fromOSString(folderPathEditor.getText());
		path = path.append(diagramNameEditor.getText()).addFileExtension(
				"docplum");
		return URI.createFileURI(path.toOSString());
	}

	public URI getDomainDiagramURI() {
		IPath path = Path.fromOSString(folderPathEditor.getText());
		path = path.append(diagramNameEditor.getText()).addFileExtension(
				"docplum_diagram");
		return URI.createFileURI(path.toOSString());
	}

	public DocPlumModelCreationPage(IStructuredSelection selection) {
		super("Domain documentation model");
		this.structuredSelection = selection;
	}

	@Override
	public void createControl(Composite parent) {
		Composite plate = new Composite(parent, SWT.NONE);
		plate.setLayout(new GridLayout(3, false));

		new Label(plate, SWT.PUSH).setText("Name:");
		diagramNameEditor = new Text(plate, SWT.SINGLE | SWT.BORDER);
		diagramNameEditor.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER,
				true, false, 2, 1));

		new Label(plate, SWT.PUSH).setText("Folder:");
		folderPathEditor = new Text(plate, SWT.SIMPLE | SWT.BORDER);
		folderPathEditor.setEditable(false);
		folderPathEditor.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false));
		Button folderPicker = new Button(plate, SWT.PUSH);
		folderPicker.setText("Browse");
		folderPicker.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER,
				false, false));

		new Label(plate, SWT.PUSH).setText("Documentation core file:");
		docCoreEditor = new Text(plate, SWT.SINGLE | SWT.BORDER);
		docCoreEditor.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false));
		Button pickDocCore = new Button(plate, SWT.PUSH);
		pickDocCore.setText("Load");
		pickDocCore.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER,
				false, false));

		new Label(plate, SWT.PUSH).setText("Plum SPL diagram:");
		plumPLEditor = new Text(plate, SWT.SINGLE | SWT.BORDER);
		plumPLEditor.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false));
		Button pickPlumSource = new Button(plate, SWT.PUSH);
		pickPlumSource.setText("Load");
		pickPlumSource.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER,
				false, false));

		// logic
		if (structuredSelection.getFirstElement() instanceof org.eclipse.core.resources.IResource) {
			String path = ((org.eclipse.core.resources.IResource) structuredSelection
					.getFirstElement()).getLocation().toOSString();
			folderPathEditor.setText(path);
		}

		ModifyListener listener = new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				setPageComplete(validatePage());
			}
		};

		plumPLEditor.addModifyListener(listener);
		docCoreEditor.addModifyListener(listener);
		diagramNameEditor.addModifyListener(listener);
		folderPathEditor.addModifyListener(listener);

		folderPicker.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				org.eclipse.ui.dialogs.ContainerSelectionDialog dialog = new org.eclipse.ui.dialogs.ContainerSelectionDialog(
						getShell(), null, true, "Select a parent:");
				dialog.setTitle("Container Selection");
				dialog.open();
				Object[] result = dialog.getResult();
				if (result.length > 0 && result[0] instanceof Path){
					String path = ResourcesPlugin
							.getWorkspace()
							.getRoot()
							.getLocation()
							.append(((Path)result[0]))
							.toOSString();
					folderPathEditor.setText(path);															
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub

			}
		});

		pickDocCore.addSelectionListener(new ShowFileDialog(docCoreEditor,
				SWT.OPEN));
		pickPlumSource.addSelectionListener(new ShowFileDialog(plumPLEditor,
				SWT.OPEN));

		// init
		setControl(plate);
	}

	protected boolean validatePage() {
		return true;
	}

	private class ShowFileDialog implements SelectionListener {

		private Text editor;

		private int style;

		public ShowFileDialog(Text editor, int style) {
			this.editor = editor;
			this.style = style;
		}

		@Override
		public void widgetSelected(SelectionEvent e) {
			FileDialog dialog = new FileDialog(getShell(), style);
			dialog.setText(docPlum.diagram.part.Messages.WizardNewFileCreationPage_SelectNewFileDialog);
			dialog.setFileName(editor.getText());
			String fileName = dialog.open();
			if (fileName != null) {
				editor.setText(fileName);
				setPageComplete(validatePage());
			}
		}

		@Override
		public void widgetDefaultSelected(SelectionEvent e) {
			// TODO Auto-generated method stub
		}
	}

}
